package com.lc.rongzecaifu.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lc.rongzecaifu.MyApplication;
import com.lc.rongzecaifu.activity.AdvertisingActivity;
import com.lc.rongzecaifu.activity.NewsActivity;
import com.lc.rongzecaifu.conn.JsonArticle;
import com.lc.rongzecaifu.rongzecaifu.R;
import com.zcx.helper.bound.BoundViewHelper;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 4/20/2016.
 */
public class ArticleAdapter extends RecyclerView.Adapter<ArticleAdapter.ViewHolder> {


    private Context context;
    private List<JsonArticle.Info.ListContent> list;

    public ArticleAdapter(Context context, List<JsonArticle.Info.ListContent> list) {
        this.context = context;
        this.list = list;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_01)
        TextView tv01;
        @Bind(R.id.iv_settings_01)
        ImageView ivSettings01;
        @Bind(R.id.ll_01)
        LinearLayout ll01;
        @Bind(R.id.view_lines)
        View view_lines;

        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_16_item_view, null)));
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        holder.tv01.setText(list.get(position).type_name);


        if (list.size() - 1 == position) {

            holder.view_lines.setVisibility(View.GONE);
        } else {

            holder.view_lines.setVisibility(View.VISIBLE);

        }


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                if (list.get(position).type_set.equals("1")) {
                    intent.putExtra("id", list.get(position).id);
                    intent.setClass(context, NewsActivity.class);
                } else if (list.get(position).type_set.equals("0")) {
                    intent.putExtra("title", list.get(position).type_name);
//                    @HttpServer("http://vhost106.zihaistar.com/api/")
                    intent.putExtra("url", "http://www.rongzecf.com/api/index/article_content?id=" + list.get(position).id + "&show_type=category");
                    intent.setClass(context, AdvertisingActivity.class);
                }
                context.startActivity(intent);
            }
        });

    }


    @Override
    public int getItemCount() {
        if (list.size()==0){
            return 0;
        }else {
            return list.size()-1;
        }


    }
}

